package pl.factat.gfk;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import pl.factat.gfk.Display.*;

/**
 * Created with IntelliJ IDEA.
 * User: adam
 * Date: 14.12.12
 * Time: 15:41
 * To change this template use File | Settings | File Templates.
 */
public class AppGui {
    private JToolBar Toolbar;
    private JPanel MainPanel;
    private JButton OpenFileButton;
    private ImagePanel imgPanel;
    private JScrollPane scrollPane;
    private JSlider widthSlider;
    private JSlider heightStlider;
    private JButton resizeButton;
    private JLabel widthLabel;
    private JLabel heightLabel;
    private JButton skewButton;
    private boolean doSkew = false;

    public AppGui() {
        OpenFileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnVal = fileChooser.showOpenDialog(MainPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    imgPanel.setImage(fileChooser.getSelectedFile().getPath());
                }
                scrollPane.updateUI();
                widthSlider.setValue(imgPanel.getImage().getWidth());
                widthLabel.setText(imgPanel.getImage().getWidth()+"");
                heightLabel.setText(imgPanel.getImage().getHeight()+"");
                heightStlider.setValue(imgPanel.getImage().getHeight());
            }
        });
        widthSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                widthLabel.setText(widthSlider.getValue() + "");
            }
        });
        heightStlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                heightLabel.setText(heightStlider.getValue() + "");
            }
        });
        resizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ResizeProcessor resizeProcessor = new ResizeProcessor();
                resizeProcessor.setNewSize(widthSlider.getValue(), heightStlider.getValue());
                imgPanel.setImage(resizeProcessor.process(imgPanel.getImage()));
                scrollPane.updateUI();
            }
        });
        skewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(doSkew) {
                    doSkew = false;
                    int[] corners;
                    corners = imgPanel.getPerspectiveCorners();
                    PerspectiveProcessor p = new PerspectiveProcessor();
                    p.setCorners(corners[0], corners[1],
                                corners[2], corners[3],
                                corners[4], corners[5],
                                corners[6], corners[7]);
                    imgPanel.setImage(p.process(imgPanel.getImage()));
                }
                else doSkew = true;
                imgPanel.setDrawPerspectiveFrame(doSkew);
            }
        });
    }

    public static void createAndShowGUI() {
        JFrame content = new JFrame("Przeksztalcenia");
        content.setContentPane(new AppGui().MainPanel);
        content.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content.pack();
        content.setSize(1000, 800);
        content.setVisible(true);

    }

    private void createUIComponents() {
        imgPanel = new ImagePanel();
        scrollPane = new JScrollPane(imgPanel);
    }
}
